import { Component, OnInit, Input, Output, EventEmitter} from '@angular/core';
import { NavController, NavParams, AlertController,ModalController } from 'ionic-angular';
import { ChoosePointsPage } from '../choose-points/choose-points';
import { Mistake } from '../mistake';
import { MistakeService } from '../mistake.service';
import { QuestionSetPage } from '../question-set/question-set';
import { SameExamplePage } from '../same-example/same-example';


@Component({
    selector: 'page-mistake-show-mistakeByTime',
    templateUrl: 'show-mistakeByTime.html',
    providers:[MistakeService],

  })
  export class showMistakeByTimePage implements OnInit{
    currentMistake:Mistake =  new Mistake("",null,"","","","","","","","","","","","")
    mistakeId:string;
    editFlag:boolean;
    editStr:string = "edit";
    starnum = 3;
    flag:boolean;
    course:string = "语文";
    pointsSelected;
    hasSourceQuestion:boolean = false;//判断是否为只有图片的错题或者是在资源库中有资源的错题
    sourceQuestion:any;//资源库中错题
    source = " ";
    starList : string[];
    blingStarPath : string;
    darkStarPath : string;
    focus : number;
    collectFlags:boolean[];
    flagIndex:number = 0;
    testCheckboxOpen: boolean;
    testCheckboxResult;

    mistake1: Mistake;

    constructor(
        public alertCtrl: AlertController,
        public modalCtrl: ModalController,
        public mistakeService: MistakeService,
        public navparams:NavParams,
        public navCtrl:NavController,
     ){
        //this.currentMistake = this.navparams.get("currentMistake");
        //this.chosenDate = this.currentDate;
        this.mistakeId = this.navparams.get("currentMistakeId");
        this.source = " ";
        this.blingStarPath = "assets/icon/star.png";
        this.darkStarPath = "assets/icon/star-2.png";
        this.starList = [];
        this.pointsSelected = [];
        this.focus = 0;
        //this.id = this.getDate()+"";
        this.collectFlags = [];
        this.mistake1 = new Mistake("",null,"","","","","","","","","","","","");
        this.mistake1 = this.currentMistake;
        for(let i = 0;i < 5;i++){
            this.starList.push(this.darkStarPath);
          }

        }

        ngOnInit():void{
           // this.currentMistake = this.navparams.get("currentMistake");
           this.mistakeId = this.navparams.get("currentMistakeId");
           this.getMistakeById();
          }


      //知识点
      presentModal() {
        if(this.editFlag == true){
          let modal = this.modalCtrl.create(ChoosePointsPage,{});
          modal.present();
          modal.onDidDismiss(pointsSelected => { this.pointsSelected = pointsSelected}
          );
          this.editFlag = true;
        }
      }

      //错误原因
      doMultiChoice(){
        if(this.editFlag == true){
        let alert = this.alertCtrl.create();
        alert.setTitle('错误原因');

        alert.addInput({
          type: 'checkbox',
          label: '计算错误',
          value: '计算错误',
          checked: true
        });

        alert.addInput({
            type: 'checkbox',
            label: '题目看错',
            value: '题目看错',
          });


        alert.addInput({
            type: 'checkbox',
            label: '掌握不牢固',
            value: '掌握不牢固',
          });

        alert.addButton('Cancel');
        alert.addButton({
          text: 'OK',
          handler: data => {
            this.testCheckboxOpen = false;
            this.testCheckboxResult = data;
            console.log(this.testCheckboxResult);
            this.testCheckboxResult.forEach(element => {
              console.log(element.value)
            });
          }
        });
        alert.present();
        this.editFlag = true;
      }
      }

      //掌握程度
      checkStar(i){
        if(this.editFlag == true){
        this.focus = i + 1;
        for(let j = 0;j < i + 1; j++){
          this.starList[j] = this.blingStarPath;
        }
        for(let j = i + 1;j<this.starList.length;j++){
          this.starList[j] = this.darkStarPath;
        }
      }
      }

      submitMistakes(){
         this.mistake1 = new Mistake(this.currentMistake.misId,null,"133","133","point19",'2',"13","13","13","13","13","","","");
         this.mistake1 = this.currentMistake;
         //this.mistake1.misId = this.currentMistake.misId;
         this.mistake1.editTime = this.getDate()+"";
         this.mistake1.points = this.pointsSelected;
         this.mistake1.reason = this.testCheckboxResult;
         this.mistake1.masterDegree = this.focus;
         console.log(this.mistake1.misId);
         this.mistakeService.submitMistakes(this.mistake1).then(data=>console.log("success"),
          error=>console.log(error));
      }

      getMistakeById(){
        this.mistakeId = this.navparams.get("currentMistakeId");
        var IDUrl = this.mistakeService.url + "/mistake/getMistakeById?mistakeId="+this.mistakeId;
        console.log(IDUrl);
        this.mistakeService.getMistakeById(IDUrl).then(mistake=>{
          this.currentMistake = mistake,
            console.log(this.currentMistake.sourceId)
            if(this.currentMistake.sourceId.toString() != "null"){
             this.mistakeService.getQuestionById(this.currentMistake.sourceId).then(question=>{
               console.log("返回错题："+question)
               this.sourceQuestion = question
               this.hasSourceQuestion = true
             })
            }
          this.focus = this.currentMistake.masterDegree;
          for(let j = 0;j < this.currentMistake.masterDegree; j++){
            this.starList[j] = this.blingStarPath;
          }
          for(let j = this.currentMistake.masterDegree;j<this.starList.length;j++){
            this.starList[j] = this.darkStarPath;
          }
        })
      }

        // 格式化日期，获取今天的日期
     getDate() {
        const Dates = new Date();
        const year: number = Dates.getFullYear();
        const month: any = ( Dates.getMonth() + 1 ) < 10 ? '0' + ( Dates.getMonth() + 1 ) : ( Dates.getMonth() + 1 );
        const day: any = Dates.getDate() < 10 ? '0' + Dates.getDate() : Dates.getDate();
        return year + '-' + month + '-' + day;
    }

    return(){
        this.navCtrl.push(QuestionSetPage,{})
    }
  }
